﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Diagnostics;

namespace WindowsPhoneApplication1
{
    public class AppSettings
    {

        IsolatedStorageSettings isolatedStore;

        const string usernameKeyName = "username";
        const string passwordKeyName = "password";

        const string usernameDefault = "";
        const string passwordDefault = "";

        static AppSettings instance = null;

        public static AppSettings Instance
        {
            get
            {
                if (instance == null)
                    instance = new AppSettings();
                return instance;
            }
        }


        private AppSettings()
        {
            try
            {
                isolatedStore = IsolatedStorageSettings.ApplicationSettings;
            }
            catch (Exception e)
            {
                //TODO: moze jakis fancy mssg box
            }
        }

        #region Functions
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            try
            {
                if (isolatedStore[Key] != value)
                {
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
            catch (KeyNotFoundException)
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }
            catch (ArgumentException)
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }
            catch (Exception e)
            {
                //TODO: kolejny fancy mssg box
            }

            return valueChanged;
        }

        public void Save()
        {
            isolatedStore.Save();
        }

        public ValueType GetValueOrDefault<ValueType>(string Key, ValueType defaultValue)
        {
            ValueType value;

            try
            {
                value = (ValueType)isolatedStore[Key];
            }
            catch (KeyNotFoundException)
            {
                value = defaultValue;
            }
            catch (ArgumentException)
            {
                value = defaultValue;
            }

            return value;
        }

        #endregion

        #region Properties

        public string Username
        {
            get
            {
                return GetValueOrDefault<string>(usernameKeyName, usernameDefault);
            }
            set
            {
                AddOrUpdateValue(usernameKeyName, value);
                Save();
            }
        }

        public string Password
        {
            get
            {
                return GetValueOrDefault<string>(passwordKeyName, passwordDefault);
            }
            set
            {
                AddOrUpdateValue(passwordKeyName, value);
                Save();
            }
        }

        #endregion
    }
}
